package tablekey

import (
	"gitee.com/tomatomeatman/golang-repository/bricks3/utils/ginutil"
	"github.com/gin-gonic/gin"
)

// @Controller 记录编号序列管理表控制器
type TableKeyController struct {
}

/**
 * 初始化
 */
func init() {
	control := &TableKeyController{}
	ginutil.AddUrl("/table/key/new", control.GetNewId, ginutil.POST)
	ginutil.AddUrl("/table/key/news", control.GetNewIds, ginutil.POST)
	ginutil.AddUrl("/table/key/reset", control.Reset, ginutil.POST)
}

// #region @Api {title=取指定表(或序列)的新Id}
// @param {name=formatLong dataType=int paramType=query explain=格式化长度(不足长度+0) required=true}
// @param {name=seriename dataType=string paramType=query explain=表名或序列名 required=true}
// @return {type=json explainType=string explain=返回新序列值}
// @RequestMapping {name=GetNewId type=POST value=/table/key/new}
// #endregion
func (control TableKeyController) GetNewId(ctx *gin.Context) (bool, string, interface{}) {
	formatLong := ginutil.GetParam(ctx, "formatLong", 0).(int)
	seriename := ginutil.GetParam(ctx, "seriename", "").(string)

	return true, "访问成功", TableKeyService{}.GetNewId(formatLong, seriename)
}

// #region @Api {title=取指定表的一批新Id}
// @param {name=formatLong dataType=int paramType=query explain=格式化长度(不足长度+0) required=true}
// @param {name=seriename dataType=string paramType=query explain=表名或序列名 required=true}
// @param {name=size dataType=int paramType=query explain=数量 required=true}
// @return {type=json explainType=string explain=返回新序列值}
// @RequestMapping {name=GetNewIds type=POST value=/table/key/news}
// #endregion
func (control TableKeyController) GetNewIds(ctx *gin.Context) (bool, string, interface{}) {
	formatLong := ginutil.GetParam(ctx, "formatLong", 0).(int)
	seriename := ginutil.GetParam(ctx, "seriename", "").(string)
	size := ginutil.GetParam(ctx, "size", 0).(int)
	return true, "访问成功", TableKeyService{}.GetNewIds(formatLong, seriename, size)
}

// #region @Api {title=重置指定表序号}
// @param {name=seriename dataType=string paramType=query explain=表名或序列名 required=true}
// @return {type=json explainType=bool, string, int> explain=返回码值}
// @RequestMapping {name=Reset type=POST value=/table/key/reset}
// #endregion
func (control TableKeyController) Reset(ctx *gin.Context) (bool, string, interface{}) {
	seriename := ginutil.GetParam(ctx, "seriename", "").(string)
	return true, "访问成功", TableKeyService{}.Reset(seriename)
}
